<?php

class MemberController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
        $this->view->getcontroller = Zend_Controller_Front::getInstance()->getRequest()->getControllerName();
        $core = new Model_Core();
        
        if ($core->isLogin()){
            $ref = $_SERVER['HTTP_REFERER'];
            $this->_redirect($ref);
        }
        $this->_helper->layout->setLayout('member');
    }
    
    protected function getProfile($uid){
        $member = new Model_CommonMembers();
        return $member->getProfile($uid);
    }
    
    public function indexAction(){
        
        $this->_redirect('/member/login');
    }

    public function loginAction()
    {
        
        $this->view->show_pic = rand(1, 4);
        $user = trim($this->_request->getParam('user'));
        $this->view->user = $user;
        
        $this->view->ref = trim($this->_request->getParam('ref'));
        
        $this->view->headTitle("用户登陆", 'PREPEND');
    }
    
    protected function getTime(){
        return time();
    }
    
    public function registerAction() {
        $core = new Model_Core();
        $type = intval($this->_request->getParam('type', 1));
        $this->view->type = $type;
        $this->view->question = $core->getConfig();
        
        $this->view->ref = $this->_request->getParam('ref');
        $submod = intval($this->_request->getParam('submod', 0));
        
        //增加FORM HASH 等验证
        if ($submod == 1){
            
            
            $data = $_POST;
            $is_check = $core->checkRegister($data);
            if ($is_check == ""){
            
                if ($data[type] == 1){
                    $company_id = 0;
                }else{
                    $save_data = array(
                        'question'      => $data[question],
                        'answer'        => "$data[answer]",
                    );
                }
            
                $ip = $_SERVER['REMOTE_ADDR'];

                
                if ($data[type] == 1){
                	$groupid = 6;//个人
                }else{
                	$groupid = 5;//普通商家
                
                	//商家详细
                	$s_data = array(
                			'coname' => $data[coname],
                			'address' => $data[address],
                	);
                	$co = new Model_CommonCo();
                	$c_id = $co->setNewCo($s_data);
                
                	$company_id = $c_id;
                }
                
                
                $save_data = array(
                    'username' 	    => "$data[username]",
                    'email'         => "$data[email]",
                    'phone'         => "$data[phone]",
                    'password'      => md5($data[password]),
                    'company_id'    => $company_id,
                    'status'        => 1,
                    'regip'         => "$ip",
                    'regdate'       => $this->getTime(),
                	'groupid' 		=> $groupid,
                );
                $db = new Model_CommonMembers();
                $id = $db->setRegisterPost($save_data);


				$data[realname] = $data[realname] ? $data[realname] : "";
				$data[idno] = $data[idno] ? $data[idno] : "";
                //PROFILE 写入
                $p_data = array(
                		'uid' => $id,
                		'realname' => $data[realname],
                		'idno'	=> $data[idno],
                );
                $profile = new Model_CommonMembersProfile();
                $p_id = $profile->setNewProfile($p_data);
                

                
                if ($id) {
                    //SET COOKIE AND GOTO MEMBER CENTER
                    $core->setLogin($id);
                    if ($data[ref] == ""){
                        $ref = urlencode("/set/profile");
                    }
                    
                    $core->sendMail($data[username], $data[email], $data[username].", 您已成功注册淘车乐会员账号", "尊敬的".$data[username].", 您好您已成功注册淘车乐会员账号, 使用此账号免费在淘车乐网站发布卖车信息！请收藏我们的网址 http://www.taochele.com <br/>微信公众账号中搜索 CNtaochele 或扫描此二维码 <img src='http://www.taochele.com/img/weixin_b.jpg'> 关注淘车乐微信公众账号获取信息先人一步！<br/> ".date('Y/m/d H:i:s', time())."淘车乐会员中心<br/>此邮件为系统自动发出,请勿回复此邮件");
                    //注册个人去 账号设置 /member/success/?username=$data[username]&ref=$data[ref]
                    $this->_redirect("/set/profile");
                    
                    //注册商家 去认证中心
                }else{
                    
                    $this->_forward('error');
                }
            
            }else{
                $this->view->geterror = $is_check;
            }
        }
                
        $this->view->headTitle("注册新账号", 'PREPEND');
    }

    public function forgotAction() {
        
        $step = intval($this->_request->getParam('step'));
        $mode = intval($this->_request->getParam('mode', 1));
                
        $member = new Model_CommonMembers();
        if ($step == 1){

            $account = trim($this->_request->getParam('loginaccount'));
            //
            if (!$account){
                $tcl_session = new Zend_Session_Namespace('taochele');
                $account = $tcl_session->forgotAccount;
            }
            $m_res = $member->getUserByAccount($account);
            $core = new Model_Core();
            
            $this->view->question = $core->getConfig();
            $this->view->m_res = $m_res;
            /*
            if (!$m_res[question]){
                $mode = 2;
            }*/
        }
        
        if ($step == 2){
            //ANSWER FORGOT
            $tcl_session = new Zend_Session_Namespace('taochele');
            $account = $tcl_session->forgotAccount;
            $auth = $tcl_session->forgotAuth;
            
            //EMAIL FORGOT
            $uid = intval($this->_request->getParam('uid'));
            $salt = trim($this->_request->getParam('salt'));
            
            
            $error = "";
            if ($uid && $salt){
                $U_res = $member->getByUid($uid);
                $is_date = date("YmdH", time()) - $U_res[forgot_date];
                if ($U_res[fotgot_status] != 1){
                    $error = "该链接仅一次有效并已被执行操作, 您不能重复执行该操作";
                }
                if ($is_date > 2){
                    $error = "您超过2小时没有执行重置新密码操作, 系统已退回该申请!";
                }
                if ($U_res[forgot_salt] != $salt){
                    $error = "来源地址不正确, 请检查您的URL地址是否完整！";
                }
                if ($error == ""){
                    
                    $tcl_session->forgotAccount = $U_res[username];
                    $tcl_session->forgotAuth = '1';
                }
                $member->setForgotUsed($uid);
            }elseif (!$account && !$auth){
                $error = "来源地址不正确, 请返回重试！";
            }
            

        }
        $this->view->error = $error;
        $step++;
        $this->view->mode = $mode;
        $this->view->step = $step;
        $this->view->headTitle("找回密码", 'PREPEND');
    }
    
    
    public function successAction() {
    
        $username = trim($this->_request->getParam('username'));
        $ref = urldecode(trim($this->_request->getParam('ref')));
        if ($username) {
            $this->view->username = $username;
            $this->view->ref = $ref;
        }else{
            $this->errorAction();
        }
    
    }
    

    
    
    //ERROR 需修正 VIEW
    public function errorAction(){
        echo '<h1>DateBase Error! </h1>';
        echo "抱歉！服务器内部错误，<a href='javascript:history.go(-1)'>返回重试</a>";
        exit();
    }
}

